package com.burgeon.backend.SHOP.rest;
/**
 * 获取提货卡
 */
import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.PickingCard;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindPICKINGCARD extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindPICKINGCARD(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */
    public ArrayList<PickingCard> getData(String phone) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<PickingCard> list = new ArrayList<PickingCard>();
        for (int i =0;i<jsonArray.length();i++){
            JSONArray array = jsonArray.getJSONArray(i);
            PickingCard pickingCard = new PickingCard();
            pickingCard.setId(array.getString(0));
            pickingCard.setDate(array.getString(1));
            pickingCard.setCode(array.getString(2));
            pickingCard.setParvalue(array.getString(3));
            pickingCard.setRemain(array.getString(4));
            pickingCard.setIsIntl(array.getString(5));
            pickingCard.setStore(array.getString(6));
            list.add(pickingCard);
        }

        return list;

    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("CREATIONDATE");//日期
        columns.add("CARDNO");//卡号
        columns.add("PARVALUE");//面值
        columns.add("REMAIN");//余额
        columns.add("ISINTL");//是否积分
        columns.add("'C_STORE_ID;NAME'");//门店

        params.put("columns", columns);
        params.put("table", "C_PICKINGCARD");//表名

        params.put("count", "true");
        HashMap paramss = new HashMap();
        String ac = "'" + code + "'";
        paramss.put("column", "'C_VIP_ID;CARDNO'");
        paramss.put("condition", ac);//查询条件

        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);
        params.put("range", "2000");

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindPICKINGCARD g = new FindPICKINGCARD("39.99.239.133:55322", "nea@burgeon.com.cn", "mgg55321");
        try {
            System.err.println(g.getData("13263459999"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
